*****************************************************
*****************Initialize dofile*******************
*****************************************************

clear all
clear matrix
set more off, perm
capture log close
cd "D:/Data/ProjData/Organizations, political participation/mod/"
do "C:/Userdata/Shared/Dofiles/DoData/Organizations, political participation/programs.do"
	
capture log close
log using "C:/Userdata/Shared/Logs/Organizations, political participation/initialize.text", replace text 


*******************************************************************************************
* This dofiles concists of the following parts:
* (1): Cleaning the data registers that we use in the project (dropping duplicates etc)
* (2): All variables are expressed as string variables. Relevant variables are destringed later
* (3): Saving relevant variables to reduce the size of the data sets 
********************************************************************************************


**Cleaning registers and saving the variables we are interested in **


* Voter turnout 2009 EU-election*
use "D:/SCB_ConPol/Stata/VD/Valdelt_09.dta", clear
tostring *, replace
dupprogram
keep LopNr kommun_id distrikt_id e Rostratt
save "Valdelt_2009", replace

*Place of residence 2000, 2001, 2008, 2009*
foreach k in 2000 2001 2008 2009 {
	use "D:/SCB_ConPol/Stata/SAMS/SAMS_2000", clear
	keep LopNr SAMS
	gen ulfyear = `k'
	gen Kommunkod = substr(SAMS,1,4)
	drop SAMS
	duplicates drop LopNr, force
	save "SAMS_`k'", replace
}
use "SAMS_2000", clear
foreach k in 2001 2008 2009 {
	append using "SAMS_`k'"
}
tostring LopNr ulfyear, replace  
save "Kommunkod_panel", replace

*LISA 1992 1993*
foreach k in 1992 1993 {
	use "D:/SCB_ConPol/Stata/LISA/LISA_`k'.dta", clear
	keep LopNr SocBidrFam SyssStatG Sun2000niva_old LoneInk 
	dupprogram
	YearsEducation
	gen socialbidragfamilj = 0
	replace socialbidragfamilj=1 if SocBidrFam > 0 & SocBidrFam !=.
	gen sysselsatt = .
	replace sysselsatt=1 if SyssStatG == 1
	replace sysselsatt=0 if SyssStatG == 5 | SyssStatG == 6
	gen ulfyear=`k'
	tostring *, replace
	save "LISA_`k'.dta", replace
}


*LISA 2000 2001*
foreach k in 2000 2001 {
	use "D:/SCB_ConPol/Stata/LISA/LISA_`k'.dta", clear
	keep LopNr SocBidrFam SyssStat Sun2000niva_old LoneInk
	dupprogram
	YearsEducation
	gen socialbidragfamilj = 0
	replace socialbidragfamilj=1 if SocBidrFam > 0 & SocBidrFam !=.
	gen sysselsatt = .
	replace sysselsatt=1 if SyssStat == 1
	replace sysselsatt=0 if SyssStat == 5 | SyssStat == 6
	gen ulfyear = `k'
	tostring *, replace
	drop SyssStat SocBidrFam
	save "LISA_`k'.dta", replace
}


* LISA 2008 2009*
foreach k in 2008 2009 {
	use "D:/SCB_ConPol/Stata/LISA/LISA_`k'.dta", clear
	keep LopNr Sun2000niva_old LoneInk SocBidrFam SyssStatJ
	dupprogram
	YearsEducation
	gen socialbidragfamilj = 0
	replace socialbidragfamilj=1 if SocBidrFam > 0 & SocBidrFam !=.
	gen sysselsatt = .
	replace sysselsatt=1 if SyssStatJ == 1
	replace sysselsatt=0 if SyssStatJ == 5 | SyssStatJ == 6
	gen ulfyear = `k' 													
	tostring *, replace
	drop SyssStatJ SocBidrFam
	save "LISA_`k'", replace
}

* LISA panel
use "LISA_1992", clear
append using "LISA_1993"
append using "LISA_2000"
append using "LISA_2001"
append using "LISA_2008"
append using "LISA_2009"
save "LISA_panel", replace

* Data on birth year/month and immigration status*
use "D:/SCB_ConPol/Stata/flergen/Fodelseuppg.dta", clear
dupprogram
keep LopNr FodArMan Kon F_delselandGrupp UtlSvBakG 
tostring LopNr, replace
save "Fodelseuppg.dta", replace


**Ulf 2000**
use "D:/SCB_ConPol/Stata/ULF/ulf2000.dta", clear
dupprogram
gen ulfyear = 2000 


/*Membership organization*/			/*Counted as number of membership in a particular cathegory. Creating binary variables*/
gen memberculture = 0
replace memberculture = 1 if MA0907 > "0" & MA0907 != "NA" 													
gen memberpension = 0
replace memberpension = 1 if MA0915 > "0" & MA0915 != "NA"
gen membersport = 0
replace membersport = 1 if (MA0903 > "0" | MA0904 > "0" | MA0905 > "0" ) & (MA0903 != "NA" | MA0904 != "NA" | MA0905 != "NA" ) 
gen memberpeace = 0
replace memberpeace = 1 if (MA0920 > "0" | MA0923 > "0") & (MA0920 != "NA" | MA0923 != "NA")
gen memberboende = 0
replace memberboende = 1 if MA0910 > "0" & MA0910 !="NA"



/*Active organization*/
gen activeculture = .
replace activeculture = 0 if MA0610 == "2"
replace activeculture = 1 if MA0610 == "1"
gen activepension = .
replace activepension = 0 if MA0074 == "2"
replace activepension = 1 if MA0074 == "1"
gen activesport = .
replace activesport = 0 if MA0605 == "2" | MA0683 == "2" | MA0688 == "2"
replace activesport = 1 if MA0605 == "1" | MA0683 == "1" | MA0688 == "1"
gen activepeace = .
replace activepeace = 0 if MA0658 == "2"
replace activepeace = 1 if MA0658 == "1"
gen activeboende = .
replace activeboende = 0 if MA0054 == "2"			
replace activeboende = 1 if MA0054 == "1"


/*EU turnout from survey*/
gen s_turnout_eu = .
replace s_turnout_eu = 1 if MA0188 == "1" 
replace s_turnout_eu = 0 if MA0188 == "2"
tostring LopNr ulfyear, replace
keep LopNr s_turnout_eu ulfyear member* active* 
save "ulf_2000.dta", replace  


*Ulf 2001*
use "D:/SCB_ConPol/Stata/ULF/ulf2001.dta", clear
dupprogram
gen ulfyear = 2001 													

/*Membership*/	
gen memberpension = 0
replace memberpension = 1 if MA0915 > "0" & MA0915 != "NA"		/*No question for culture organization for this survey.*/
gen membersport = 0												/*Divided into friluftsföreningen, lagsport och annan idrottsföreningen for this year*/
replace membersport = 1 if (MA0903 > "0" | MA0904 > "0" | MA0905 > "0") & (MA0903 != "NA" | MA0904 != "NA" | MA0905 != "NA" )  
gen memberpeace = 0
replace memberpeace = 1 if MA0920 > "0" & MA0920 != "NA"		
gen memberboende = 0
replace memberboende = 1 if MA0910 > "0" & MA0910 !="NA"

gen s_turnout_eu = .

/*Active*/
gen activepension = .
replace activepension = 0 if MA0074 == "2"
replace activepension = 1 if MA0074 == "1"
gen activesport = .
replace activesport = 0 if MA0604 == "2" | MA0683 == "2" | MA0688 == "2"
replace activesport = 1 if MA0604 == "1" | MA0683 == "1" | MA0688 == "1"
gen activepeace = .
replace activepeace = 0 if MA0649 == "2"
replace activepeace = 1 if MA0649 == "1" 
gen activeboende = .
replace activeboende = 0 if MA0054 == "2"			
replace activeboende = 1 if MA0054 == "1"


/*EU turnout from survey*/
replace s_turnout_eu = 1 if MA0188 == "1" 
replace s_turnout_eu = 0 if MA0188 == "2"
tostring LopNr ulfyear, replace
keep LopNr s_turnout_eu ulfyear member* active* 
save "ulf_2001.dta", replace  



*Ulf 2008-2009*
foreach k in 2008 2009 {
	use "D:/SCB_ConPol/Stata/ULF/ulf`k'.dta", clear
	dupprogram
	gen ulfyear = `k' 													
	tostring *, replace
	rename MA0600 membersport
	rename MA0601 activesport
	rename MA0648 memberpeace
	rename MA0050 memberboende
	rename MA0649 activepeace 
	rename MA0609 memberculture
	rename MA0610 activeculture
	rename MA0070 memberpension
	rename MA0074 activepension
	rename MA0054 activeboende
	rename BA0020 agesurvey
	foreach j in memberboende memberculture memberpension membersport memberpeace activeculture activepension activesport activepeace activeboende  {
		replace `j' = "." if `j' == "NA"
		destring `j', replace
		replace `j'=. if `j' == 8 												/*Do not know*/
		replace `j' =. if `j' == 9 												/*Do not want to answer*/
		replace `j' = 0 if `j' == 2		
		}
	keep LopNr ulfyear agesurvey member* active*
	save "ulf_`k'.dta", replace 
}




* Political discussions *
forvalues k=1992(1)2010 {
	use "D:/SCB_ConPol/Stata/ULF/ulf`k'.dta", clear  
	gen ulfyear = `k'
	tempfile ulf`k'
	capture tostring MA0200, replace
	capture gen MA0200 = ""
	keep LopNr MA0200 ulfyear
	save `ulf`k'', replace
}

use `ulf1992', clear
forvalues k=1993(1)2010 {
	append using `ulf`k''
}

gen poldisc = real(MA0200) if !inlist(MA0200,"8","9","NA")
tostring LopNr, replace
label var poldisc "Discussing politics"
save "polintULF.dta", replace

**Inhabitants per km2**
import excel "D:\Data\ExtData\Invkm2\invkm2Kommun.xlsx", sheet("use") firstrow allstring clear
destring y*, replace

reshape long y, i(Kod) j(year)
rename y invkm2
rename Kod Kommunkod
rename year ulfyear
tostring ulfyear, replace
drop Kommun
save "invkm2.dta", replace

*******
clear all
capture log close
